<template>
  <div class="jobs">
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="全部工作" name="全部工作">
        <AllJobs v-if="activeName === '全部工作'"/>
      </el-tab-pane>
      <el-tab-pane label="我接取的" name="我接取的">
        <StudentCurrentJobs v-if="activeName === '我接取的'"/>
      </el-tab-pane>
      <el-tab-pane label="我发布的" name="我发布的">

      </el-tab-pane>
    </el-tabs>
  </div>
</template>
<script>
import AllJobs from "./component/AllJobs.vue";
import StudentCurrentJobs from './component/StudentCurrentJobs.vue'
import {getUserProfile} from "@/api/system/user";
export default {
  components: {AllJobs, StudentCurrentJobs},
  data(){
    return {
      activeName: '全部工作',
      //当前用户信息
      userInfo: {
        address: '',
        privateKey: '',
        studentId: ''
      },
    }
  },
  created() {
    this.getUserInfo()
  },
  methods: {
    handleClick(val){
      this.activeName = val.label
    },
    //获取个人信息
    async getUserInfo(){
      const res = await getUserProfile()
      const remark = res.data.remark
      this.userInfo.studentId = res.data.userId
      if(remark.indexOf('已开户') !== -1){
        const {address, privateKey} = await this.forMartAddressAnprivateKey(remark)
        this.userInfo.address = address || ''
        this.userInfo.privateKey = privateKey || ''
        this.$store.commit('app/SET_USER_INFO', this.userInfo)
      }else{
        this.$confirm('请先创建账户!', '提示', {
          confirmButtonText: '确定',
          callback: action => {
            if(action === 'confirm'){
              this.$router.push('/index')
            }
          }
        });
      }
    },
    //提取个人私钥和地址
    async forMartAddressAnprivateKey(remark){
      const regex = /\{.*\}/;
      const match = remark.match(regex);
      if (match) {
        const jsonStr = match[0];
        try {
          const obj = JSON.parse(jsonStr);
          return obj
        } catch (error) {
          throw new Error('无效的 JSON 对象字符串');
        }
      } else {
        this.$message.error('请先创建账户!')
        throw new Error('请先创建账户!');
      }
    },
  }
}
</script>
<style scoped lang="scss">
.jobs{
  padding: 14px;
}
</style>
